package com.lxy.demo.servlet;

import com.lxy.demo.entity.EdocCategory;
import com.lxy.demo.entity.EdocEntry;
import com.lxy.demo.service.EdocCategoryService;
import com.lxy.demo.service.EdocCategoryServiceImpl;
import com.lxy.demo.service.EdocEntryService;
import com.lxy.demo.service.EdocEntryServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet(name = "Servlet", urlPatterns = "/main")
public class Servlet extends HttpServlet {

    static EdocCategoryService ecs = new EdocCategoryServiceImpl();
    static EdocEntryService ees = new EdocEntryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String cmd = request.getParameter("cmd");
        if (cmd != null && cmd.equals("main")){
            main(request,response);
        }
        if (cmd != null && cmd.equals("login")){
            login(request,response);
        }
        if (cmd != null && cmd.equals("insert")){
            insert(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String seid = request.getParameter("seid");
        List<EdocEntry> eeList = ees.findEdocEntry(seid);
        //System.out.println(eeList);
        request.setAttribute("eeList",eeList);
        request.setAttribute("seid",seid);
        request.getRequestDispatcher("page/main.jsp").forward(request,response);
    }


    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        List<EdocCategory> ecList = ecs.findEdocCategory();
        request.getSession().setAttribute("ecList",ecList);
        //System.out.println(ecList);
        //request.getRequestDispatcher("page/main.jsp").forward(request,response);
        response.sendRedirect(request.getContextPath()+"/main?cmd=main");
    }

    protected void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        EdocEntry ed = new EdocEntry();

        DateConverter dc = new DateConverter();
        //设置转换格式
        dc.setPatterns(new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"});
        //实现注册转换
        ConvertUtils.register(dc, Date.class);

        try {
            BeanUtils.populate(ed,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        System.out.println(ed);
        boolean b = ees.insertInfo(ed);
        String a = "";
        if (b){
            a = "添加成功";
            request.setAttribute("insert",a);
        }else {
            a = "添加失败";
            request.setAttribute("insert",a);
        }

        request.getRequestDispatcher("page/insert.jsp").forward(request,response);
    }
}
